package com.ckx.controller;

import com.ckx.domain.ResponseResult;
import com.ckx.domain.dto.MenuDto;
import com.ckx.domain.vo.MenuByRoleAddVo;
import com.ckx.domain.vo.MenuVo;
import com.ckx.domain.vo.RoleUpdateVo;
import com.ckx.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult list(String menuName, Integer status) {

        List<MenuVo> menuVos = menuService.selectAllMenus(menuName, status);
        return ResponseResult.okResult(menuVos);

    }

    @PostMapping
    public ResponseResult addMenu(@RequestBody MenuDto menuDto) {
        menuService.addMenu(menuDto);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult getMenuInfo(@PathVariable Long id) {

        MenuVo menuVo = menuService.getMenuInfo(id);
        return ResponseResult.okResult(menuVo);

    }

    @PutMapping
    public ResponseResult updateMenu(@RequestBody MenuDto menuDto) {
        menuService.updateMenu(menuDto);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult delMenu(@PathVariable Long id) {
        menuService.del4DelFlag(id);
        return ResponseResult.okResult();
    }

    @GetMapping("/treeselect")
    public ResponseResult treeSelect() {
        List<MenuByRoleAddVo> menus = menuService.treeSelect();
        return ResponseResult.okResult(menus);
    }

    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeSelect(@PathVariable Long id) {
        //角色信息
        List<MenuByRoleAddVo> menus = menuService.treeSelect();
        //角色当前权限
        List<Long> checkedKeys = menuService.roleMenuTreeSelect(id);
        return ResponseResult.okResult(new RoleUpdateVo(menus, checkedKeys));
    }

}
